package com.yyh.service.impl;


import com.yyh.dao.UserDaoImpl;
import com.yyh.domain.User;
import com.yyh.service.UserService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

/**
 *  用户接口的实现类
 */
public class UserServiceImpl implements UserService {
    public static final Scanner INPUT = new Scanner(System.in);

    UserDaoImpl userDao = new UserDaoImpl();
    /**
     * 用户注册功能
     * @param
     */
    @Override
    public void register() {

        LinkedList<User> list = userDao.getData();
        outer:while (true) {
            // 1. 输入用户名和密码  并 进行数据格式校验
            System.out.print("请输入用户名：");
            String userName = INPUT.next();
            if(userName == null || userName.length() == 0 || "".equals(userName)){
                System.out.println("用户名不能为空！");
                return;
            }
            // 判断用户名是否重复
            boolean flag = false;
            inner:for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                if(userName.equals(user.getUserName())){
                    flag = true;
                    break inner;
                }
            }
            if(flag == true){
                System.out.println("用户名已经存在！");
                continue outer;
            }else { // 能注册
                while (true) {
                    System.out.print("请输入密码：");
                    String password = INPUT.next();
                    if(password.length() < 6 || password == null || "".equals(password)){
                        System.out.println("密码必须有值并且长度大于或者等于6");
                        continue ;
                    }
                    else {
                        System.out.print("请再次输入密码：");
                        String rePassword = INPUT.next();

                        if(!password.equals(rePassword)){
                            System.out.println("两次输入的密码不一致");
                            continue ;
                        }
                    }

                    // 输入年龄
                    System.out.print("请输入年龄：");
                    Integer age = INPUT.nextInt();
                    if(age < 0 || age > 100){
                        System.out.println("您输入的年龄必须是0-100之间的数字");
                        System.out.print("请重新输入年龄：");
                        age = INPUT.nextInt();
                    }
                    // 输入昵称
                    System.out.print("请输入昵称");
                    String niChen = INPUT.next();

                    // 封装对象
                    User user = new User(userName, password, niChen, age);

                    userDao.add(user);
                    System.out.println("注册成功");
                    return;
                }
            }
        }
    }

    /**
     * 用户登录功能
     */
    @Override
    public void login() {
        String username = null;
        String password = null;
        while (true) {
            System.out.print("请输入用户名：");
            username = INPUT.next();
            System.out.print("请输入密码：");
            password = INPUT.next();
            if (password.length() < 6){
                System.out.println("密码长度要大于等于6！");
                continue;
            }
            else {
                break;
            }
        }
        // 将用户名和密码封装到User对象中
        User user = new User(username, password, null, null);

        // 判断是否登录成功
        User loginUser = userDao.selectUserByUsernameAndPassword(user);


        if(loginUser == null){ // 登录失败
            System.out.println("用户名或者密码错误！");
            return;
        }
        else{ // 登录成功
            // 进入系统, 来到系统的欢迎页面
            userDao.successHtml(loginUser);
        }

    }
}
